//
//  LSFiltrateCompetitionVController.m
//  LookScore
//
//  Created by wr on 2018/1/26.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSFiltrateCompetitionVController.h"
#import "LSFiltrateCompetitionCell.h"
#import "LSCustomHeaderView.h"
//#import "LSExponentModel.h"
//#import "LSCompetitionModel.h"
#import "ZJScrollPageView.h"
#import "LSFiltrateSubViewController.h"
//#import "LSAIHistoricaModel.h"
#import "LSDataLeagueModel.h"
@interface LSFiltrateCompetitionVController ()<UICollectionViewDelegate,UICollectionViewDataSource,ZJScrollPageViewDelegate>

@property(strong,nonatomic)UICollectionView *collectionView;

@property(strong,nonatomic)NSMutableArray *competitionArray;

@property (nonatomic, assign) BOOL allBtnSelect;

@property(strong, nonatomic)NSArray<NSString *> *titles;
@property (nonatomic, strong) ZJScrollPageView *scrollPageView;
@property (nonatomic, assign) NSInteger topViewH;
@property (nonatomic, strong) UIButton *tmpButton;
@property (nonatomic, strong) UILabel * label;
@end

@implementation LSFiltrateCompetitionVController{
    LSFiltrateSubViewController * IMVC;
    UIButton * allBtn;
    
}


- (NSMutableArray *)competitionArray{
    if (!_competitionArray) {
        _competitionArray = @[].mutableCopy;
    }
    
    return _competitionArray;
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing=0;
        flowLayout.minimumInteritemSpacing=0;
        flowLayout.itemSize=CGSizeMake(LSScreenWidth/4,36);
        _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0,35, LSScreenWidth,LSScreenHeight -64) collectionViewLayout:flowLayout];
//        _collectionView.userInteractionEnabled=YES;
//        flowLayout.headerReferenceSize = CGSizeMake(LSScreenWidth, 50);
//        flowLayout.footerReferenceSize = CGSizeMake(LSScreenWidth, 50);
       _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
      
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.title=@"筛选赛事";
    self.view.backgroundColor = LSTitleWhiteColor;
    if (self.filtrateType==LSFiltrateCompetitionIsCompetitionType) {
        [self initPagerView];
        
        
    }else{
        _topViewH = 0;
        
//        NSMutableIndexSet * indexset = [NSMutableIndexSet indexSet];
//        for (NSInteger i = 0; i < self.leagueArray.count; i++) {
//            for (NSInteger j = i + 1; j < self.leagueArray.count; j++) {
//                LSExponentModel * tempModel = self.leagueArray[i];
//                LSExponentModel * model = self.leagueArray[j];
//                if ([tempModel.LeagueName isEqualToString:model.LeagueName]) {
//                    [indexset addIndex:i];
//                }
//            }
//        }
//        [self.leagueArray removeObjectsAtIndexes:indexset];
//
//        //判断数组里面的模型的isSelected 是否全是yes 全为yes sectionHeader上的按钮为选中状态.
//        for (LSExponentModel * model in self.leagueArray) {
//            if (model.isSelected == YES) {
//
//                _allBtnSelect = YES;
//            }else{
//
//                _allBtnSelect = NO;
//                break;
//            }
//        }
//
//        //判断数组里面的模型isSelected是否为yes  是yes加到名字数组中
//        for (LSExponentModel * model in self.leagueArray) {
//            if (model.isSelected) {
//                [self.competitionArray addObject:model.LeagueName];
//            }
//        }
//
//        self.collectionView.frame = CGRectMake(0, _topViewH, LSScreenWidth, LSScreenHeight - 64-80);
//        [self.collectionView registerClass:[LSFiltrateCompetitionCell class] forCellWithReuseIdentifier:NSStringFromClass([LSFiltrateCompetitionCell class])];
//            [self.view addSubview:self.collectionView];
//        if (@available(iOS 11.0, *)) {
//            self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
//
//        }
//        [self createView];
//        allBtn.selected = _allBtnSelect;
//        if (_allBtnSelect) {
//            allBtn.backgroundColor = LSSubColorFF9E44;
//            _tmpButton = allBtn;
//        }
//
//    }
        
//        NSMutableIndexSet * indexset = [NSMutableIndexSet indexSet];
//        for (NSInteger i = 0; i < self.leagueArray.count; i++) {
//            for (NSInteger j = i + 1; j < self.leagueArray.count; j++) {
//                LSExponentList * tempModel = self.leagueArray[i];
//                LSExponentList * model = self.leagueArray[j];
//                if ([tempModel.LeagueName isEqualToString:model.LeagueName]) {
//                    [indexset addIndex:i];
//                }
//            }
//        }
//        [self.leagueArray removeObjectsAtIndexes:indexset];
        
        //判断数组里面的模型的isSelected 是否全是yes 全为yes sectionHeader上的按钮为选中状态.
        for (LSDataLeagueModel * model in self.leagueArray) {
            if (model.isBtnSelected == YES) {
                
                _allBtnSelect = YES;
            }else{
                
                _allBtnSelect = NO;
                break;
            }
        }
        
        //判断数组里面的模型isSelected是否为yes  是yes加到名字数组中
        for (LSDataLeagueModel * model in self.leagueArray) {
            if (model.isBtnSelected) {
                [self.competitionArray addObject:model.LeagueName_CN];
            }
        }
        
        self.collectionView.frame = CGRectMake(0, _topViewH, LSScreenWidth, LSScreenHeight - LSNavBarHeight-80);
        [self.collectionView registerClass:[LSFiltrateCompetitionCell class] forCellWithReuseIdentifier:NSStringFromClass([LSFiltrateCompetitionCell class])];
        [self.view addSubview:self.collectionView];
        if (@available(iOS 11.0, *)) {
            self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
            
        }
        [self createView];
        allBtn.selected = _allBtnSelect;
        if (_allBtnSelect) {
            allBtn.backgroundColor = LSSubColorFF9E44;
            _tmpButton = allBtn;
        }
        
    }
        
        
        
}

- (void)createView{
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = LSTitleWhiteColor;
    [self.view addSubview:bgView];
    bgView.sd_layout
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .bottomSpaceToView(self.view, 0)
    .heightIs(80);
    
    UILabel * label = [[UILabel alloc] init];
    label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.leagueArray.count - self.competitionArray.count];
    label.font = LSFontSize12;
    label.textColor = LSTitleGrayColor2;
    [bgView addSubview:label];
    _label = label;
    label.sd_layout
    .leftSpaceToView(bgView, 10)
    .topSpaceToView(bgView, 10)
    .autoHeightRatio(0);
    [label setSingleLineAutoResizeWithMaxWidth:180];
    
    allBtn = [[UIButton alloc] init];
    
    allBtn.titleLabel.font=LSFontSize13;
    allBtn.layer.cornerRadius=3;
    allBtn.layer.masksToBounds=YES;
    allBtn.layer.borderColor = LSTitleGrayColor3.CGColor;
    allBtn.layer.borderWidth=.5f;
    allBtn.backgroundColor = [UIColor whiteColor];
    [allBtn setTitle:@"全选" forState:UIControlStateNormal];
    [allBtn setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [allBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [allBtn addTarget:self action:@selector(allSelectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:allBtn];
    
    allBtn.sd_layout
    .rightSpaceToView(bgView, 10)
    .centerYEqualToView(label)
    .heightIs(25)
    .widthIs(60);
    
    UIButton * afterBtn = [[UIButton alloc] init];
    
    afterBtn.titleLabel.font=LSFontSize13;
    afterBtn.layer.cornerRadius=3;
    afterBtn.layer.masksToBounds=YES;
    afterBtn.layer.borderColor = LSTitleGrayColor3.CGColor;
    afterBtn.layer.borderWidth=.5f;
    [afterBtn setTitle:@"反选" forState:UIControlStateNormal];
    afterBtn.backgroundColor = [UIColor whiteColor];
    
    [afterBtn setTitleColor:LSTitleGrayColor3 forState:UIControlStateNormal];
    [afterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [afterBtn addTarget:self action:@selector(afterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:afterBtn];
    
    afterBtn.sd_layout
    .rightSpaceToView(allBtn, 10)
    .centerYEqualToView(label)
    .heightIs(25)
    .widthIs(60);
    
    UIButton * sureBtn = [[UIButton alloc] init];
    sureBtn.layer.cornerRadius = 5;
    [sureBtn addTarget:self action:@selector(sureBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:LSButtonColor];
    [sureBtn setTitleColor:LSTitleWhiteColor forState:UIControlStateNormal];
    sureBtn.titleLabel.font = LSFontSize12;
    [bgView addSubview:sureBtn];
    sureBtn.sd_layout
    .leftSpaceToView(bgView, 10)
    .rightSpaceToView(bgView, 10)
    .heightIs(30)
    .topSpaceToView(label, 15);
}


-(void)initPagerView{
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    
    style.scrollLineColor=LSNavMainColor;
    
    style.segmentHeight=35;
    
    style.autoAdjustTitlesWidth=YES;
    
    style.normalTitleColor=[UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:255.f/255.f alpha:1];
    
    style.selectedTitleColor=LSNavTitlenColor;
    
    style.scrollLineColor=LSNavTitlenColor;
    
    self.titles = @[@"全部", @"滚球"];
    // 初始化
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,self.view.bounds.size.height) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [_scrollPageView setSelectedIndex:0 animated:NO];
    _scrollPageView.segmentView.backgroundColor=LSNavMainColor;
    [self.view addSubview:_scrollPageView];
}
- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    if (!childVc) {
        
        IMVC = [[LSFiltrateSubViewController alloc] init];
        KWeakSelf(self);
        IMVC.block = ^(NSMutableArray *leaNameArrry,NSMutableArray *zouLeaNameArray,NSInteger type) {
            if (weakself.competitionBlock) {
                
              weakself.competitionBlock(leaNameArrry,zouLeaNameArray, type);
            }
        };
        IMVC.gunqiuDataArray = self.zouArray.mutableCopy;
        IMVC.allDataArray = self.leaArray.mutableCopy;
        childVc = IMVC;
    }
    
    return childVc;
}

- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods {
    return NO;
}


#pragma mark 🔥 UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.filtrateType==LSFiltrateCompetitionIsCompetitionType) {
        return self.compitionArray.count;
    }
    return self.leagueArray.count;
}

/** cell的内容*/
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LSFiltrateCompetitionCell * cell = [LSFiltrateCompetitionCell FiltrateCompetitionCellBycollectionView:collectionView indexPath:indexPath];
    if (self.filtrateType == LSFiltrateCompetitionIsCompetitionType) {
        
        cell.comModel = self.compitionArray[indexPath.row];
    }else{
        
       cell.comModel = self.leagueArray[indexPath.row];
    }
   
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LSFiltrateCompetitionCell * cell =  (LSFiltrateCompetitionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    
    cell.competitionBu.selected=!cell.competitionBu.selected;
    if (self.filtrateType == LSFiltrateCompetitionIsCompetitionType) {
//        LSDataLeagueModel * model = self.compitionArray[indexPath.row];
//        if (cell.competitionBu.selected) {
//            model.isBtnSelected = YES;
//            cell.competitionBu.backgroundColor = LSSubColorFF9E44;
//            cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
//            [self.competitionArray addObject: model.LeagueName_CN];
//        }else{
//            model.isBtnSelected = NO;
//            [self.competitionArray removeObject:model.LeagueName_CN];
//            cell.competitionBu.backgroundColor = [UIColor whiteColor];
//            cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
//        }
    }else{
//        LSExponentModel * model = self.leagueArray[indexPath.row];
//        if (cell.competitionBu.selected) {
//            model.isSelected = YES;
//            cell.competitionBu.backgroundColor = LSSubColorFF9E44;
//            cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
//            [self.competitionArray addObject: model.LeagueName];
//        }else{
//            model.isSelected = NO;
//            [self.competitionArray removeObject:model.LeagueName];
//            cell.competitionBu.backgroundColor = [UIColor whiteColor];
//            cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
//
//
//        }
        
        LSDataLeagueModel * model = self.leagueArray[indexPath.row];
        if (cell.competitionBu.selected) {
            model.isBtnSelected = YES;
            cell.competitionBu.backgroundColor = LSSubColorFF9E44;
            cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
            [self.competitionArray addObject: model.LeagueName_CN];
        }else{
            model.isBtnSelected = NO;
            [self.competitionArray removeObject:model.LeagueName_CN];
            cell.competitionBu.backgroundColor = [UIColor whiteColor];
            cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
            
            
        }
         _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.leagueArray.count - self.competitionArray.count];
    }
   
    
    MyLog(@"%@",self.competitionArray);
    
}


- (void)sureBtnClicked:(UIButton *)sender{
   
        if (self.filtrateType==LSFiltrateCompetitionIsCompetitionType) {
            
            self.block(self.competitionArray);
        }else{
            self.block(self.competitionArray);
        }
        [self.navigationController popViewControllerAnimated:YES];
    
}



- (void)allSelectBtnAction:(UIButton *)sender{
    _tmpButton.backgroundColor = [UIColor whiteColor];
    _tmpButton.selected = sender.selected;
    _tmpButton = sender;
    sender.selected = YES;
    
    sender.backgroundColor = LSSubColorFF9E44;
    [self.competitionArray removeAllObjects];
    if (self.filtrateType == LSFiltrateCompetitionIsCompetitionType) {
        
//        [self.compitionArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            LSDataLeagueModel * model = obj;
//            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
//            [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
//            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
//            cell.competitionBu.selected=!cell.competitionBu.selected;
//            if (sender.selected) {
//                model.isBtnSelected = YES;
//                [self.competitionArray addObject: model.LeagueName_CN];
//                cell.competitionBu.backgroundColor = LSSubColorFF9E44;
//                cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
//            }else{
//
//                model.isBtnSelected = NO;
//                [self.competitionArray removeObject:model.LeagueName_CN];
//                cell.competitionBu.backgroundColor = [UIColor whiteColor];
//                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
//            }
//
//        }];

    }else{
        
        [self.leagueArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            LSExponentModel * model = obj;
            LSDataLeagueModel * model = obj;
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            cell.competitionBu.selected=!cell.competitionBu.selected;
            if (sender.selected) {
                model.isBtnSelected = YES;
                [self.competitionArray addObject: model.LeagueName_CN];
                cell.competitionBu.backgroundColor = LSSubColorFF9E44;
                cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
            }else{

                model.isBtnSelected = NO;
                [self.competitionArray removeObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
            }
            
             _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.leagueArray.count - self.competitionArray.count];
        }];
    }
    [self.collectionView reloadData];
    
}


- (void)afterBtnClicked:(UIButton *)sender{
    
    _tmpButton.backgroundColor = [UIColor whiteColor];
    _tmpButton.selected = sender.selected;
    _tmpButton = sender;
    sender.selected = YES;
    sender.backgroundColor = LSSubColorFF9E44;
    [self.competitionArray removeAllObjects];
    [self.leagueArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            LSExponentModel * model = obj;
        LSDataLeagueModel * model = obj;
            NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
            LSFiltrateCompetitionCell * cell = (LSFiltrateCompetitionCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
            cell.competitionBu.selected=!cell.competitionBu.selected;
            if (model.isBtnSelected) {
                model.isBtnSelected = NO;
                [self.collectionView deselectItemAtIndexPath:indexPath animated:YES];
                [self.competitionArray removeObject:model.LeagueName_CN];
                cell.competitionBu.backgroundColor = [UIColor whiteColor];
                cell.competitionBu.layer.borderColor = LSTitleGrayColor3.CGColor;
            }else{
                model.isBtnSelected = YES;
                NSIndexPath * indexPath = [NSIndexPath indexPathForItem:idx inSection:0];
                [self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:0];
                [self.competitionArray addObject: model.LeagueName_CN];
                cell.competitionBu.backgroundColor = LSSubColorFF9E44;
                cell.competitionBu.layer.borderColor = LSSubColorFF9E44.CGColor;
                
                
            }
         _label.text = [NSString stringWithFormat:@"共隐藏:%ld",self.leagueArray.count - self.competitionArray.count];
        }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
